//
//  main.m
//  05-HomeWork
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 daming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
#if 0
//    一、
//    1.创建1个包含5个字符串元素的数组；
//    2.用for循环倒序输出所有元素；
//    3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    
    NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];
    for (int i = 4; i >= 0; i--) {
        NSString *str = arr[i];
        NSLog(@"%@", str);
    }
    NSString *appendStr = [NSString string];
    for (NSString *string in arr) {
        appendStr = [appendStr stringByAppendingString:string];
    }
        NSLog(@"%@", appendStr);
#endif
#if 0
//    二、
//    1.创建1个包含5个person对象的数组(姓名用英文)；
//    2.用for循环找到年龄最大的Person对象；
//    3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    
    Person *per0 = [[Person alloc]initWithName:@"zhangsan" age:50 sex:@"female"];
    Person *per1 = [[Person alloc]initWithName:@"lisi" age:20 sex:@"male"];
    Person *per2 = [[Person alloc]initWithName:@"wanger" age:60 sex:@"female"];
    Person *per3 = [[Person alloc]initWithName:@"xiaoming" age:17 sex:@"male"];
    Person *per4 = [[Person alloc]initWithName:@"zhaoyi" age:710 sex:@"male"];
    NSArray *perArray = @[per0, per1, per2, per3, per4];
    NSInteger temp = 0;
    for (int i = 0; i < 5; i++) {
        Person *per = perArray[i];
        if (per.age > temp) {
            temp = per.age;
        }
    }
    for (int i = 0; i < 5; i++) {
        Person *maxPer = perArray[i];
        if (maxPer.age == temp) {
            NSLog(@"per%d的年龄最大", i);
        }
    }
    for (Person *perMax in perArray) {
        if (perMax.age > 50) {
           perMax.name = [perMax.name stringByAppendingString:@"-brother"];
        }
    }
    NSLog(@"%@", perArray);
//    三、
//    1.使用第二题目中生成的数组；
//    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//    3.使用NSSortDescriptor，按姓名进行排序。
    
    NSArray *nameArr = [perArray sortedArrayUsingSelector:@selector(deSortedByName:)];
    NSArray *ageArr = [perArray sortedArrayUsingSelector:@selector(deSortedByAge:)];
    NSArray *sexArr = [perArray sortedArrayUsingSelector:@selector(deSortedBySex:)];
    NSLog(@"nameArr = %@", nameArr);
    NSLog(@"ageArr = %@", ageArr);
    NSLog(@"sexArr = %@", sexArr);
    NSSortDescriptor *sortOfname = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    NSArray *arrOfName = [perArray sortedArrayUsingDescriptors:@[sortOfname]];
    NSLog(@"arrOfName = %@", arrOfName);
#endif
#if 1
//    四、
//    处理省市区文件中的area.txt,将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)
  
//    将路径的内容转换为字符串
    NSString *strOfUrl = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
//    将字符串用 "\n" 分割 放入数组中
    NSArray *arrString = [strOfUrl componentsSeparatedByString:@"\n"];
//    创建三个可变的数组容器(分别用来存放省, 市, 区)
    NSMutableArray *arrOfProvince = [NSMutableArray array];
    NSMutableArray *arrOfCity = [NSMutableArray array];
    NSMutableArray *arrOfDistrict = [NSMutableArray array];
//    遍历数组, 将省, 市, 区分别放入对应的数组中
    for (NSString *string in arrString) {
        if (string.length > 0) {
            if (![string containsString:@"  "]) {
                [arrOfProvince addObject:string];
            } else if ([[string substringWithRange:NSMakeRange(0, 4)]isEqualTo:@"    "]) {
                [arrOfDistrict addObject:string];
            } else {
                [arrOfCity addObject:string];
            }
        }
    }
//    创建一个可变数组接收刨除了省的内容
    NSMutableArray *arrOfCityAndDistrict = [NSMutableArray array];
    for (NSString *string in arrString) {
        if (string.length > 0) {
            if (![string containsString:@"  "]) {
            } else {
                [arrOfCityAndDistrict addObject:string];
            }
        }
    }
//    用于打印刨除了省, 只剩下市和区的数组
//    NSLog(@"%@", arrOfCityAndDistrict);
//    创建一个数组用来接收市的下标
    NSMutableArray *arrOfNumber = [NSMutableArray array];
    NSInteger cityNumber = 0;
    for (NSInteger i = 0; i < arrOfCity.count; i++) {
        for (NSInteger j = 0; j < arrOfCityAndDistrict.count; j++) {
            if ([arrOfCity[i] isEqualToString:arrOfCityAndDistrict[j]]) {
                cityNumber = j;
            }
        }
//    将长整形转换成字符串, 以便放入数组中
        NSString *str = [NSString stringWithFormat:@"%ld", cityNumber];
        [arrOfNumber addObject:str];
    }
//    打印市的下标数组
//    NSLog(@"%@", ArrOfNumber);
    
//    创建一个数组, 用来接收每个市的区
    NSArray *disArr = [NSArray array];
//    创建可变字典, 以市为key, 区的数组为value
    NSMutableDictionary *dicOfCityAndDis = [NSMutableDictionary dictionary];
//    循环遍历, 提取出对应的key(市)和value(区的数组) 放入字典中
    for (int i = 0; i < arrOfNumber.count; i++) {
        if (i + 1 == arrOfNumber.count) {
            disArr = [arrOfCityAndDistrict subarrayWithRange:NSMakeRange([arrOfNumber[i] integerValue] + 1, [arrOfNumber[i] integerValue] - [arrOfNumber[i - 1] integerValue] - 1)];
        } else {
            disArr = [arrOfCityAndDistrict subarrayWithRange:NSMakeRange([arrOfNumber[i] integerValue] + 1, [arrOfNumber[i + 1] integerValue]- [arrOfNumber[i] integerValue] - 1)];
        }
//    打印每个市所对应的区
//    NSLog(@"%@", dicArr);
        
//    添加到字典中
        [dicOfCityAndDis setObject:disArr forKey:arrOfCity[i]];
    }
//    打印市和区的字典
//    NSLog(@"%@", dicOfCityAndDis);
//    创建可变数组, 用来存放省和市
    NSMutableArray *arrOfProAndCity = [NSMutableArray array];
//    循环遍历, 刨除区, 只留下含有市和省的数组
    for (NSString *string in arrString) {
        if (string.length > 0) {
            if ([[string substringWithRange:NSMakeRange(0, 3)]isEqualTo:@"   "]) {
                
            } else {
                [arrOfProAndCity addObject:string];
            }
        }
    }
//    NSLog(@"%@", arrOfProAndCity);
//    创建数组, 用来接收省的下标
    NSMutableArray *proNumberArr = [NSMutableArray array];
    NSInteger proNumber = 0;
//    提取出省在只含有省和市的数组中的下标, 添加到数组中
    for (NSInteger i = 0; i < arrOfProvince.count; i++) {
        for (NSInteger j = 0; j < arrOfProAndCity.count; j++) {
            if ([arrOfProvince[i] isEqualToString:arrOfProAndCity[j]]) {
                proNumber = j;
            }
        }
//    将长整形转换成字符串, 以便放入数组中
        NSString *str = [NSString stringWithFormat:@"%ld", proNumber];
        [proNumberArr addObject:str];
    }
    NSArray *cityArr = [NSArray array];
//    创建可变字典, 用于存放以省为key, 市为value
    NSMutableDictionary *dicOfProvince = [NSMutableDictionary dictionary];
//    循环遍历, 提取出对应的key(省)和value(市的数组) 放入字典中
    for (int i = 0; i < proNumberArr.count; i++) {
        if (i == proNumberArr.count - 1) {
            cityArr = [arrOfProAndCity subarrayWithRange:NSMakeRange([proNumberArr[i] integerValue] + 1,arrOfProAndCity.count - [proNumberArr[i] integerValue] -1)];
        } else {
        cityArr = [arrOfProAndCity subarrayWithRange:NSMakeRange([proNumberArr[i] integerValue] + 1, [proNumberArr[i + 1] integerValue] - [proNumberArr[i] integerValue] - 1)];
        }
//    打印每个省所对应的市的数组
//    NSLog(@"%@", cityArr);
//    循环将key(省)value(市的数组)放入字典中
        [dicOfProvince setObject:cityArr forKey:arrOfProvince[i]];
    }
//    打印key为省, value为市的数组的字典
//    NSLog(@"%@", dicOfProvince);
    
//    打印只含有省的数组
//    NSLog(@"省 = %@", arrOfProvince);
//    打印只含有市的数组
//    NSLog(@"市 = %@", arrOfCity);
//    打印只含有区的数组
//    NSLog(@"区 = %@", arrOfDistrict);
#endif
#if 0
//    五、拓展
//    上述作业做完, 处理Location1.txt
//    要求同第四题。自己寻找规律

//    提取字符串
    NSString *strOfLocation = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
//    打印字符串
//    NSLog(@"%@", strOfLocation);
    NSArray *arrOfFile = [strOfLocation componentsSeparatedByString:@","];
//    打印存放通过字符串分割得到的数组
//    NSLog(@"%@", arrOfFile);
//    创建三个数组, 分别存放省, 市, 区
    NSMutableArray *arrOfProvince = [NSMutableArray array];
    NSMutableArray *arrOfCity = [NSMutableArray array];
    NSMutableArray *arrOfDistrict = [NSMutableArray array];
    NSArray *arrOfSepar = [NSArray array];
    for (NSString *str in arrOfFile) {
        arrOfSepar = [str componentsSeparatedByString:@"|"];
        if ([str hasSuffix:@"0000"]) {
            [arrOfProvince addObject:str];
        } else if ([arrOfSepar[1] hasSuffix:@"00"] && ![str hasSuffix:@"0000"]) {
            [arrOfCity addObject:str];
        } else {
            [arrOfDistrict addObject:str];
        }
    }
    NSLog(@"省都有%@", arrOfProvince);
    NSLog(@"市都有%@", arrOfCity);
    NSLog(@"区都有%@", arrOfDistrict);
    
    

    
    

    
#endif
    return 0;
}
